package com.liu.lab06.dao.impl;

import com.liu.lab06.dao.MusicDao;
import com.liu.lab06.pojo.Music;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class MusicDaoImpl implements MusicDao {

    // Spring 提供的数据库操作模板类 JdbcTemplate，由外部注入（如通过 Spring 配置）
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }



    /**
     * 根据主键 ID 查询一条音乐记录
     */
    @Override
    public Music findMusicById(int id) {
        String sql = "SELECT * FROM tb_music WHERE id = ?";
        RowMapper<Music> rowMapper = BeanPropertyRowMapper.newInstance(Music.class);
        return jdbcTemplate.queryForObject(sql, rowMapper, id);
    }

    /**
     * 查询所有音乐记录
     */
    @Override
    public List<Music> findAllMusics() {
        String sql = "SELECT * FROM tb_music";
        RowMapper<Music> rowMapper = BeanPropertyRowMapper.newInstance(Music.class);
        return jdbcTemplate.query(sql, rowMapper);
    }

    /**
     * 根据音乐名模糊查询
     */
    @Override
    public List<Music> findMusicsByName(String key) {
        String sql = "SELECT * FROM tb_music WHERE musicname LIKE ?";
        RowMapper<Music> rowMapper = BeanPropertyRowMapper.newInstance(Music.class);
        String pattern = "%" + key + "%"; // 模糊匹配条件
        return jdbcTemplate.query(sql, rowMapper, pattern);
    }

    /**
     * 根据分类 ID 查询音乐
     */
    @Override
    public List<Music> findMusicsByCategory(int category_id) {
        String sql = "SELECT * FROM tb_music WHERE category_id = ?";
        RowMapper<Music> rowMapper = BeanPropertyRowMapper.newInstance(Music.class);
        return jdbcTemplate.query(sql, rowMapper, category_id);
    }

    /**
     * 添加音乐记录
     */
    @Override
    public int addMusic(Music music) {
        String sql = "INSERT INTO tb_music (musicname, singer, ranking, category_id) VALUES (?, ?, ?, ?)";
        Object[] params = new Object[]{
                music.getMusicname(),
                music.getSinger(),
                music.getRanking(),
                music.getCategory_id()
        };
        return jdbcTemplate.update(sql, params);
    }

    /**
     * 更新音乐信息
     */
    @Override
    public int updateMusic(Music music) {
        String sql = "UPDATE tb_music SET musicname = ?, singer = ?, ranking = ?, category_id = ? WHERE id = ?";
        Object[] params = new Object[]{
                music.getMusicname(),
                music.getSinger(),
                music.getRanking(),
                music.getCategory_id(),
                music.getId()
        };
        return jdbcTemplate.update(sql, params);
    }

    /**
     * 根据 ID 删除音乐
     */
    @Override
    public int deleteMusic(int id) {
        String sql = "DELETE FROM tb_music WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }
}
